package cn.myzqu.sign.project.server.service.impl;

import cn.myzqu.sign.project.common.vo.ProjectInfoVo;
import cn.myzqu.sign.project.server.base.exception.CustomException;
import cn.myzqu.sign.project.server.dao.OsResourceMapper;
import cn.myzqu.sign.project.server.dao.ProjectMapper;
import cn.myzqu.sign.project.server.dao.ProjectStaffMapper;
import cn.myzqu.sign.project.server.entity.*;
import cn.myzqu.sign.project.server.service.ProjectService;
import cn.myzqu.sign.project.common.input.ProjectStaffQueryInput;
import cn.myzqu.sign.project.common.input.ProjectQueryInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: 的川
 * @Date: 2019/3/5 16:39
 */
@Service
@Slf4j
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ProjectStaffMapper projectStaffMapper;

    @Autowired
    private OsResourceMapper resourceMapper;

    /**
     * 发布项目
     * @param projectInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = CustomException.class)
    public Boolean add(ProjectInfo projectInfo){
        if(projectInfo.getPoster()==null){
            OsResource osResource = resourceMapper.selectRandomOne();
            projectInfo.setPoster(osResource.getUrl());
        }
        int result = projectMapper.insert(projectInfo);
        log.info("result:"+result);
        //添加人员
        if(result>0){
            ProjectStaff projectStaff = new ProjectStaff();
            log.info(projectInfo.toString());
            projectStaff.setProjectInfoId(projectInfo.getId().toString());
            projectStaff.setUserId(projectInfo.getCreateUserId());
            projectStaff.setStatus((byte)1);
            projectStaff.setUserRole((byte)1);
            projectStaff.setIsSign((byte)0);
            if(projectStaffMapper.insert(projectStaff)>0){
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public ProjectInfo getProjectInfoById(Long id){
        return projectMapper.selectById(id);
    }

    @Override
    public List<Map> searchProject(ProjectQueryInput projectQueryInput){
        return projectMapper.searchProject(projectQueryInput);
    }

    @Override
    public List<Map> getProjectByStaff(Map params){
        return projectMapper.getProjectByStaff(params);
    }

    @Override
    public List<ProjectInfoVo> searchMyProject(ProjectStaffQueryInput projectStaffQueryInput){
        return projectMapper.searchMyProject(projectStaffQueryInput);
    }
}
